<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <template #moreQty="{ column, record }">
        <div v-if="column.dataIndex == 'moreQty'">
          <a @click="orderMoreAdd(record.id)" style="display: flex; line-height: 30px">
            <Icon
              icon="ant-design:fast-forward-outlined"
              style="margin-right: 10px; font-size: 30px"
            />
            {{ record.moreQty ? record.moreQty : '无' }}
          </a>
          <!-- <a>111</a> -->
        </div>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.type"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon v-if="item.icon" :icon="item.icon" /> {{ t(item.title) }}
        </a-button>
      </template>
    </BasicTable>
    <!-- <OverDrawerForm @register="registerOverDrawer" @success="handleSuccess" /> -->
    <FormTabs @register="registerDrawer" @success="handleSuccess" />
    <!-- 补领数 -->
    <BasicDrawer
      v-bind="$attrs"
      :showFooter="false"
      @register="registerLLDrawer"
      width="80%"
      title="领料记录管理"
      @close="handleSuccess"
    >
      <MoreList :orderFlow="orderFlow" />
    </BasicDrawer>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsTestTestDataList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { t } from '/@/hooks/web/useI18n';
  import { BasicTable, useTable, ActionItem, BasicColumn } from '/@/components/Table';
  import { customListData } from '/@/api/test/testData';
  import { listSet } from '/@/api/test/testData';
  import { FormProps } from '/@/components/Form';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useDrawer } from '/@/components/Drawer';
  import FormTabs from './formTabs.vue';
  import { updateCstatus } from '/@/api/m8/machine/over/taskOverForm';
  import MoreList from '/@/views/m8/machine/more/list.vue';
  import { BasicDrawer } from '/@/components/Drawer';
  import { handleExport } from '/@/utils/export';

  const { showMessage } = useMessage();
  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度3
    schemas: [
      // {}
    ],
  });
  let orderFlow = ref<any>();
  let tableColumns = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let butData = ref<any>([]);
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });
  let title = ref('');
  const [registerDrawer, { openDrawer: openLLDrawer }] = useDrawer();
  const [registerLLDrawer, { openDrawer }] = useDrawer();

  const actionColumn: BasicColumn = {
    width: 150, //操作按钮宽度
    actions: (record: Recordable) => {
      actionColumnData.value.forEach((item) => {
        if (item.title == '领料') {
          item.label = '领料';
          anth: 'm8:machine:over:edit';
          item.onClick = () => handleLLForm(record);
        }
        if (item.title == '关闭') {
          item.label = '关闭';
          item.popConfirm = {
            title: '确认要关闭领料吗？',
            confirm: handleDelete.bind(this, { id: record.id, cstatus: 1 }),
          };
          anth: 'm8:machine:over:edit';
        }
      });
      return actionColumnData.value;
    },
  };

  const [registerTable, { reload, setProps, getForm }] = useTable({
    actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
  });

  onMounted(async () => {
    const res = await listSet({ viewCode: 'M8_taskOver_More_list' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);
    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头
    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮
    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.btnKey === 'btnExport') {
        item.do = () => btnExport();
      }
    });

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      api: customListData,
      beforeFetch: (params) => {
        params.url = listTabConfig.value.api;
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
    });
    if (!listTabConfig.value.immediate) {
      reload();
    }
  });

  // 领料记录管理
  function orderMoreAdd(record) {
    orderFlow.value = record;
    openDrawer(true, {
      overId: record.id,
    });
  }

  // 刷新表格数据（含表单回调）
  function handleSuccess() {
    reload();
  }

  // 领料表单
  function handleLLForm(record) {
    openLLDrawer(true, {
      overId: record.id,
      taskCode: record.taskCode,
      machineId: record.machineId,
    });
  }

  //关闭领料
  async function handleDelete(record: Recordable) {
    const res = await updateCstatus(record);
    showMessage(res.message);
    handleSuccess();
  }

  //导出数据
  function btnExport() {
    var formdata = getForm().getFieldsValue();
    handleExport('/m8/machine/over/exportDataMoreList', formdata);
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
